#include "gdn/Application.h"

namespace gdn
{
	Application::Application()
	{
	}

	Application::Application( const Vector2i& windowSize, unsigned int windowBpp, const std::string& windowTitle )
	{
		Construct( windowSize, windowBpp, windowTitle );
	}

	Application::~Application()
	{
		Destruct();
	}

	int Application::Run()
	{
		Scene* activeScene = scenes.FindScene( scenes.GetActiveSceneName() );

		if ( activeScene == NULL )
		{
			std::cout << "The scene '" << GetActiveSceneName() << "' was not found." << std::endl;
			return -1;
		}

		isRunning = true;
		timer.Reset();
		while ( isRunning )
		{
			if ( timer.GetElapsedTime() >= 1 / PHYSICS_FPS )
			{
				//std::cout << timer.GetElapsedTime() << " >= 1 / " << PHYSICS_FPS << " ( " << 1 / PHYSICS_FPS << " ) " << std::endl;
				activeScene->Step( window );
				activeScene->Draw( window );
				timer.Reset();

				activeScene = scenes.FindScene( scenes.GetActiveSceneName() );
			}
		}

		return 0;
	}

	void Application::Close()
	{
		isRunning = false;
		window.Close();
	}

	Window& Application::GetWindow()
	{
		return window;
	}

	void Application::AddScene( const std::string& name, Scene* scene )
	{
		scenes.AddScene( name, scene );
	}

	void Application::RemoveScene( const std::string& name )
	{
		scenes.RemoveScene( name );
	}

	bool Application::DoesSceneExist( const std::string& name ) const
	{
		return scenes.DoesSceneExist( name );
	}

	std::vector< std::string > Application::GetSceneList() const
	{
		return scenes.GetSceneList();
	}

	Scene* Application::FindScene( const std::string& name ) const
	{
		return scenes.FindScene( name );
	}

	void Application::SetActiveScene( const std::string& name )
	{
		scenes.SetActiveScene( name );
	}

	const std::string& Application::GetActiveSceneName() const
	{
		return scenes.GetActiveSceneName();
	}

	void Application::Construct( const Vector2i& windowSize, unsigned int windowBpp, const std::string& windowTitle )
	{
		window.Create( windowSize, windowBpp, windowTitle );
		isRunning = true;
	}

	void Application::Destruct()
	{
		Close();
	}
}
